/* Clear all existing data from memory */
clear all

/* Load the matched sample dataset */
use "$output\matched_sample_nn.dta"

/* Keep only observations within the analysis period (2014-2019) */
keep if mdate < mofd(date("20200101","YMD"))
keep if mdate > mofd(date("20131231","YMD"))

/* Keep only firms in the event window of -12 to +24 months */
keep if deltamonths >= -12
keep if deltamonths <= 24

/* Generate financial variables */
gen loan = stloan + ltloan + bm + bi + oc  /* Total loan amount */
gen leasing = bm + bi  /* Leasing component */
gen lloan = log(loan)  /* Log of total loan */
gen lstloan = log(stloan)  /* Log of short-term loan */
gen lavailable = log(oc)  /* Log of available credit */
gen ldrawn = log(stloan + ltloan + leasing)  /* Log of drawn credit */
gen lleasing = log(leasing)  /* Log of leasing */
gen clusage = co / (co + oc)  /* Credit line usage ratio */
gen lltloan = log(ltloan + bm + bi)  /* Log of long-term loan */
gen lco = log(co)  /* Log of credit line */
gen lother = log(stloan + oc)  /* Log of other loans */

/* Close any open logs and start a new log */
capture log close
log using "$logs\trend_visual_psm.log", replace

/* TREND RELATIVE coef plot Figure 3a */
preserve

/* Convert deltamonths into a non-negative categorical variable */
replace delta = deltamonths + 12

/* Define labels for deltamonths categories */
capture lab_delta drop 
forvalues n = 0(1)36 {
	local displayed_value = `n' - 12
	dis "`displayed_value'"
	label define lab_delta `n'  "`displayed_value'", add
}
label val delta lab_delta

/* Generate trend coefficient plots */
foreach var in lloan {
	/* Run regression with fixed effects and clustering at firm level */
	qui eststo `var' : reghdfe `var' i1.p2p##ib11.delta [pweight=weight], a(i.siren#i.year mdate) cluster(siren)  

	/* Figure 3a */
	/* Generate coefficient plot */
	coefplot (`var', ci(95) mcolor(emerald%60) msize(medsmall) lwidth(medthin) ///
	ciopts(recast(rcap) color(gs7%90) fcolor(gs7%90) lpattern(dash))), ///
	omitted base keep(1.p2p#*) rename(1.p2p#*= "") vertical yline(0, lpattern(shortdash) lcolor(grey)) ///
	xlabel(2 "-11" 4 "-9" 6 "-7" 8 "-5" 10 "-3" 12 "-1" 14 "1" 16 "3" 18 "5" 20 "7" 22 "9" 24 "11" 26 "13" 28 "15" 30 "17" 32 "19" 34 "21" 36 "23", labsize(small)) ///
	xsize(6) ysize(4) ylabel(, nogrid) ylabel(-0.2(0.1)0.3) ///
	plotregion(fcolor(white) lcolor(white)) graphregion(color(white)) xline(12) ytitle("")

	/* Save the graphs */
	graph save "$figures/Figure_3a_Trend_relative_`var'_coefplot_psm_nn.gph", replace
	graph export "$figures/Figure_3a_Trend_relative_`var'_coefplot_psm_nn.pdf", replace

	/* Run second regression with different fixed effects */
	qui eststo `var'2 : reghdfe `var' i1.p2p##ib11.delta [pweight=weight], a(i.siren mdate) cluster(siren)  

	/* Figure D2a */
	/* Generate second coefficient plot */
	coefplot (`var'2, ci(95) mcolor(emerald%60) msize(medsmall) lwidth(medthin) ///
	ciopts(recast(rcap) color(gs7%90) fcolor(gs7%90) lpattern(dash))), ///
	omitted base keep(1.p2p#*) rename(1.p2p#*= "") vertical yline(0, lpattern(shortdash) lcolor(grey)) ///
	xlabel(2 "-11" 4 "-9" 6 "-7" 8 "-5" 10 "-3" 12 "-1" 14 "1" 16 "3" 18 "5" 20 "7" 22 "9" 24 "11" 26 "13" 28 "15" 30 "17" 32 "19" 34 "21" 36 "23", labsize(small)) ///
	xsize(6) ysize(4) ylabel(, nogrid) ylabel(-0.2(0.1)0.3) ///
	plotregion(fcolor(white) lcolor(white)) graphregion(color(white)) xline(12) ytitle("")

	/* Save the second set of graphs */
	graph save "$figures/Figure_D2a_Trend_relative_`var'_coefplot_psm_nn2.gph", replace
	graph export "$figures/Figure_D2a_Trend_relative_`var'_coefplot_psm_nn2.pdf", replace
}

/* Generate trend coefficient plots for additional variables */
foreach var in lco lltloan lother clusage {
	
	/* Figure 4a-b, left + Figure C2a */
	qui eststo `var' : reghdfe `var' i1.p2p##ib11.delta [pweight=weight], a(i.siren#i.year mdate) cluster(siren)  

	coefplot (`var', ci(95) mcolor(emerald%60) msize(medsmall) lwidth(medthin) ///
	ciopts(recast(rcap) color(gs7%90) fcolor(gs7%90) lpattern(dash))), ///
	omitted base keep(1.p2p#*) rename(1.p2p#*= "") vertical yline(0, lpattern(shortdash) lcolor(grey)) ///
	xlabel(2 "-11" 4 "-9" 6 "-7" 8 "-5" 10 "-3" 12 "-1" 14 "1" 16 "3" 18 "5" 20 "7" 22 "9" 24 "11" 26 "13" 28 "15" 30 "17" 32 "19" 34 "21" 36 "23", labsize(small)) ///
	xsize(6) ysize(4) ylabel(, nogrid) ylabel(-0.3(0.1)0.4) ///
	plotregion(fcolor(white) lcolor(white)) graphregion(color(white)) xline(12) ytitle("")

	graph save "$figures/Trend_relative_`var'_coefplot_psm_nn.gph", replace
	graph export "$figures/Trend_relative_`var'_coefplot_psm_nn.pdf", replace

	/* Run second regression with different fixed effects */
	qui eststo `var'2 : reghdfe `var' i1.p2p##ib11.delta [pweight=weight], a(i.siren mdate) cluster(siren)  

	coefplot (`var'2, ci(95) mcolor(emerald%60) msize(medsmall) lwidth(medthin) ///
	ciopts(recast(rcap) color(gs7%90) fcolor(gs7%90) lpattern(dash))), ///
	omitted base keep(1.p2p#*) rename(1.p2p#*= "") vertical yline(0, lpattern(shortdash) lcolor(grey)) ///
	xsize(6) ysize(4) ylabel(, nogrid) ylabel(-0.3(0.1)0.4) ///
	plotregion(fcolor(white) lcolor(white)) graphregion(color(white)) xline(12) ytitle("")

	graph save "$figures/Trend_relative_`var'_coefplot_psm_nn2.gph", replace
	graph export "$figures/Trend_relative_`var'_coefplot_psm_nn2.pdf", replace
}
restore

/* Close log file */
log close
